layui.use(['form'], function() {

	var form = layui.form;
	form.render();

	function login(account, passWord) {
		$.ajax({
			type: "post",
			url: "http://localhost:8080/ssmService/index/login", //请求的接口路径
			async: true,
			data: {
				"user_account": account,
				"user_password": passWord
			}, //设置参数
			//ajax执行之前
			"beforeSend": function() {
				layer.load(1, {
					shade: [0.5, 'grey']
				});
			},
			//ajax执行成功后的回调函数
			"success": function(result) {
				//result为Ajax所响应的数据
				console.log(result);
				if(result.resultCode == 0) {
					localStorage.setItem("userId", result.data.user_id);
					localStorage.setItem("userName", result.data.name);
					localStorage.setItem("headImg", result.data.head_img_url);
					location.href = "views/index.html";
				} else {
					layer.msg("登录失败", {
						icon: 2,
						time: 1000,
						top: 300,
						left: 500
					});
				}
			},
			"error": function() {
				layer.msg("未知错误,请刷新页面", {
					icon: 2,
					time: 1000,
					top: 300,
					left: 500

				});
			}
		});
	}

	form.on('submit(login)', function() {
		var account = $("#account").val();
		var passWord = $("#passWord").val();

		var ras_passWord = rsaSecurity(passWord);

		login(account, ras_passWord);
		if($("#remember").is(":checked")) {
			localStorage.setItem("account", account);
			localStorage.setItem("passWord", passWord);
		} else {
			localStorage.setItem("account", "");
			localStorage.setItem("passWord", "");
			//localStorage.clear();
		}
		return false; //组织表单跳转，如果需要表单跳转，去掉这段
	});

	function remembers() {
		if(localStorage.getItem("account") != "" && localStorage.getItem("passWord") != "") {
			$("#account").val(localStorage.getItem("account"));
			$("#passWord").val(localStorage.getItem("passWord"));
			$("#remember").prop("checked", true);
		}
		form.render();
	}
	remembers();

	function addUnclock() {
		var engLish = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
		var attr = [];
		var code = "";
		for(var i = 0; i < 4; i++) {
			var ranNum = Math.ceil(Math.random() * 25);
			if(attr.indexOf(ranNum) == -1) {
				attr.push(ranNum);
			} else {
				i--;
			}
			console.log(attr);
		}
		console.log(attr);

		for(var i = 0; i < 4; i++) {
			code += engLish[attr[i]];
			console.log(engLish[attr[i]]);

		}
		$(".captcha_gbws_wrap").remove();
		$(".login").buttonCaptcha({
			codeWord: code, //匹配的验证码
			codeZone: code,
			captchaHeader: "请正确的将字符移动到框内",
			captchaUnlocked: "通过验证"
		});
		$(".captcha_gbws_wrap").appendTo("#verifygroup");
	}
	addUnclock();
	//rsa加密
	function rsaSecurity(password) {
		console.log(password);
		var rsa_str;
		$.ajax({
			type: "get",
			url: "http://localhost:8080/ssmService/rsa/generateRSAJsKey",
			async: false,
			"success": function(result) {
				//从服务端获取加密模板
				var modulus = result.split(";")[0];
				//获取公钥指数
				var public_exponent = result.split(";")[1];
				//公钥指数，加密模板
				var public_key = new RSAUtils.getKeyPair(public_exponent, "", modulus);
				//对登录密码进行颠倒
				var rsa_pass = password.split("").reverse().join("");
				//获取到加密字符串 参数（公钥，颠倒后的密码）
				rsa_str = RSAUtils.encryptedString(public_key, rsa_pass);
			}
		});

		return rsa_str;
	}

});